package com.xwyzsn.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwyzsn.food.entity.User;
import com.xwyzsn.food.mapper.UserMapper;
import com.xwyzsn.food.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author scorpios
 * @since 2024-03-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper baseMapper;

    public Object get(Map<String,?> payload) {
        return null;
    }

    public Object deleteOne(Map<String,?> payload) {
        return null;
    }

    public Object addOne(Map<String,?> payload) {
        User entity = new User();

        if(payload.containsKey("username")){
            String username = String.valueOf(payload.get("username"));
            entity.setUsername(username);
        }  if (payload.containsKey("password")){
            String password = String.valueOf(payload.get("password"));
            entity.setPassword(password);
        }  if (payload.containsKey("tel")){
            String email = String.valueOf(payload.get("tel"));
            entity.setTel(email);
        }  if (payload.containsKey("role")){
            String phone = String.valueOf(payload.get("role"));
            entity.setRole(phone);
        }
        return baseMapper.insert(entity);

    }

    public User login(Map<String,?> payload) {
        String username = String.valueOf(payload.get("username"));
        String password = String.valueOf(payload.get("password"));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        User user = baseMapper.selectOne(wrapper);
        if(user.getPassword().equals(password)){
            return user;
        }
        return null;
    }
}
